package com.atguigu.gmall.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.CategoryMapper;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryCategoriesByPid(Long pid) {

        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        if (pid != -1){
            queryWrapper.eq("parent_id",pid);
        }

        return this.list(queryWrapper);
    }

    @Override
    public List<CategoryEntity> queryCategorysWithSub(Long pid) {
        return this.categoryMapper.queryCategoriesByPid(pid);
    }

    @Override
    public List<CategoryEntity> queryCategoriesByCid3(Long cid3) {

        //用cid3 查询到三级分类，再用三级分类的父id查询到二级分类，然后用二级分类的父id作为id查询到一级分类
        CategoryEntity categoryEntity3 = this.getById(cid3);

        Long cid2 = categoryEntity3.getParentId();
        CategoryEntity categoryEntity2 = this.getById(cid2);

        Long cid1 = categoryEntity2.getParentId();
        CategoryEntity categoryEntity1 = this.getById(cid1);

        return Arrays.asList(categoryEntity1,categoryEntity2,categoryEntity3);
    }

}